package Tool;

import Javabean.User;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

public class Function_User_Add {
    public Function_User_Add(Connection conn, ArrayList<User> users){
        //创建一个弹框对象
        JDialog jDialog = new JDialog();
        new InitJdialog(jDialog);
        jDialog.setSize(350,250);
        //让弹框居中
        jDialog.setLocationRelativeTo(null);
        jDialog.setTitle("添加管理员");

        //设置组件
        JTextField usertxt = new JTextField();
        JPasswordField passwordtxt = new JPasswordField();
        JPasswordField repasswordtxt = new JPasswordField();

        JLabel user = new JLabel("用户名");
        JLabel password = new JLabel("密码");
        JLabel repassword = new JLabel("重新输入密码");

        JButton truejbt = new JButton("确定");

        user.setBounds(20,14,70,20);
        password.setBounds(23,54,70,20);
        repassword.setBounds(3,94,100,20);

        usertxt.setBounds(80,15,200,20);
        passwordtxt.setBounds(80,57,200,20);
        repasswordtxt.setBounds(80,97,200,20);

        truejbt.setBounds(135,150,70,30);


        //添加组件
        jDialog.getContentPane().add(user);
        jDialog.getContentPane().add(password);
        jDialog.getContentPane().add(repassword);

        jDialog.getContentPane().add(usertxt);
        jDialog.getContentPane().add(passwordtxt);
        jDialog.getContentPane().add(repasswordtxt);

        jDialog.getContentPane().add(truejbt);

        //添加监听
        truejbt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                try {
                    //获取输入数据
                    //创建一个User对象
                    User user = new User();
                    user.setID(usertxt.getText());
                    user.setPassWord(passwordtxt.getText());

                    String s1 = passwordtxt.getText();
                    String s2 = repasswordtxt.getText();
                    String s3 = usertxt.getText();


                    //判断数据是否正确
                    if(!((s1.equals(s2)) && !(s3.equals("")) && !(s1.equals(""))) ){

                        //如果输入不正确，执行catch
                        int a = 1 / 0;

                    }


                    //添加到数据库中
                    MySQL_UserAdd.userAdd(conn,user);

                    //添加到集合中
                    Arraylist_User_Add.userAdd(users,user);

                } catch (Exception ex) {

                    //输入数据错误处理
                    JDialog errorjDialog = new JDialog();
                    new Init_ShuRuError_Jdialog(errorjDialog);

                    throw new RuntimeException(ex);
                }

                //创建成功的提醒弹框
                JDialog Throw = new JDialog();
                new InitJdialog(Throw);
                Throw.setBounds(600,100,200,130);
                JLabel truethrow = new JLabel("添加成功");
                truethrow.setBounds(65,20,80,30);
                Throw.getContentPane().add(truethrow);

                Throw.setVisible(true);

                //关闭弹窗
                jDialog.dispose();

            }
        });


        //让弹框显示出来
        jDialog.setVisible(true);
    }
}
